package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Reply;
import com.hs.sfzy.service.ReplyService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ReplyController
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月24日 20:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    /**
     * 添加数据
     *
     * @param questionId
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */

    @GetMapping("/findByQuestionId")
    public Result selectByQuestionId(Long questionId) {
        List<Reply> list = replyService.selectByQuestionId(questionId);
        return Result.success(list);
    }


    @PostMapping
    public Result insert(@RequestBody Reply reply) {
        int num = replyService.insert(reply);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        int num = replyService.delete(id);
        if (num != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 修改数据
     *
     * @param reply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */
    @PutMapping
    public Result update(@RequestBody Reply reply) {
        int num = replyService.update(reply);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 查询所有数据
     *
     * @param reply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 20:44
     */
    @GetMapping
    public Result selectAll(Reply reply) {
        List<Reply> list = replyService.selectAll(reply);
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:45
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Reply reply = replyService.selectById(id);
        return Result.success(reply);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param reply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:45
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Reply reply) {
        PageInfo<Reply> info = replyService.selectByPageInfo(pageNum, pageSize, reply);
        return Result.success(info);
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Long userId){
        List<Reply> replies = replyService.findById(userId);
        return Result.success(replies);
    }
}
